data {
  int<lower=0> n;        // number of obs
  int<lower=0> m;        // number of studies
  int<lower=0> nX;       // number of individual-level parameters
  int<lower=0> nZ;	      // number of study-level parameters
  matrix[n,nX] X;        // predictor matrix indiviual level
  matrix[m,nZ] Z;        // predictor matrix study level
  vector[n] svo;	        // dependent variable
  int<lower=0> study[n]; // study indicator
}
parameters {
  vector[m] beta0;
  vector[nX] beta;
  vector[nZ] gamma;
  real<lower=0> sigma_beta0;
  real<lower=0> sigma_beta;
  real<lower=0> sigma_svo;
} 
model {
  vector[n] svo_hat;

  for (i in 1:n)
    svo_hat[i] <- beta0[study[i]] + X[i] * beta;
  beta0 ~ normal(Z * gamma, sigma_beta0);
  svo ~ normal(svo_hat, sigma_svo);
  
  beta ~ normal(0,1);
  gamma ~ normal(0,1);
  //sigma_beta0 ~ cauchy(0,5);
  //sigma_svo ~ cauchy(0,5);
}
